//
//  CaseHistoryViewController.m
//  JiuLe
//
//  Created by 黄鑫 on 16/8/30.
//  Copyright © 2016年 Fine. All rights reserved.
//

#import "CaseHistoryViewController.h"
#import "CaseHistoryModel.h"

@interface CaseHistoryViewController ()
@property (strong, nonatomic) NSMutableArray *arrDataSource;
@end

@implementation CaseHistoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"病史";
    [self showNavgationWithLeftItemWithTitle:@"返回"];
    [self showNavBarWithRightImage:[UIImage imageNamed:@"icon_save"] andRightBtnAction:@selector(save) LeftBtnImage:nil andLeftBtnAction:nil title:nil barColor:nil];
    NSDictionary *dict = [self.response objectForKey:@"result"];
    NSMutableArray *arr = [NSMutableArray arrayWithArray:[dict objectForKey:@"medical_history"]];
    for (int i = 0 ; i < self.arrDataSource.count; i++) {
        for (int j = 0 ; j < arr.count ;j ++) {
            CaseHistoryModel *model = _arrDataSource[i];
            if ([model.name isEqualToString:arr[j]]) {
                model.select = YES;
                [_arrDataSource replaceObjectAtIndex:i withObject:model];
                break;
            }
        }
    }
    [self.tableView reloadData];
}

#pragma  mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _arrDataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"caseVCCellID" forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"caseVCCellID"] ;
    }
    UILabel *labName = [cell.contentView viewWithTag:1];
    UIImageView *image = [cell.contentView viewWithTag:2];
    CaseHistoryModel *model = _arrDataSource[indexPath.row];
    labName.text = model.name;
    if (model.select) {
        image.hidden = NO;
    }else{
        image.hidden = YES;
    }
    return cell;
}

#pragma mark - UITableview Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CaseHistoryModel *model = _arrDataSource[indexPath.row];
    model.select = !model.select;
    [_arrDataSource replaceObjectAtIndex:indexPath.row withObject:model];
    [self.tableView reloadData];
}


#pragma mark - set/get
- (NSMutableArray *)arrDataSource{
    if (_arrDataSource == nil) {
        _arrDataSource = [NSMutableArray array];
        CaseHistoryModel *model = [[CaseHistoryModel alloc]init];
        model.name = @"肿瘤";
        model.select = NO;
        [_arrDataSource addObject:model];
        
        CaseHistoryModel *model1 = [[CaseHistoryModel alloc]init];
        model1.name = @"脑血管";
        model1.select = NO;
        [_arrDataSource addObject:model1];
        
        CaseHistoryModel *model2 = [[CaseHistoryModel alloc]init];
        model2.name = @"心脏病";
        model2.select = NO;
        [_arrDataSource addObject:model2];
        
        CaseHistoryModel *model3 = [[CaseHistoryModel alloc]init];
        model3.name = @"糖尿病";
        model3.select = NO;
        [_arrDataSource addObject:model3];
        
        CaseHistoryModel *model4 = [[CaseHistoryModel alloc]init];
        model4.name = @"肝部疾病";
        model4.select = NO;
        [_arrDataSource addObject:model4];
        
        CaseHistoryModel *model5 = [[CaseHistoryModel alloc]init];
        model5.name = @"肺部疾病";
        model5.select = NO;
        [_arrDataSource addObject:model5];
        
        CaseHistoryModel *model6 = [[CaseHistoryModel alloc]init];
        model6.name = @"高血压";
        model6.select = NO;
        [_arrDataSource addObject:model6];
        
        CaseHistoryModel *model7 = [[CaseHistoryModel alloc]init];
        model7.name = @"老年痴呆";
        model7.select = NO;
        [_arrDataSource addObject:model7];
        
        CaseHistoryModel *model8 = [[CaseHistoryModel alloc]init];
        model8.name = @"精神病";
        model8.select = NO;
        [_arrDataSource addObject:model8];
        
        CaseHistoryModel *model9 = [[CaseHistoryModel alloc]init];
        model9.name = @"胃病";
        model9.select = NO;
        [_arrDataSource addObject:model9];
        
        CaseHistoryModel *model10 = [[CaseHistoryModel alloc]init];
        model10.name = @"低血糖";
        model10.select = NO;
        [_arrDataSource addObject:model10];
        
        CaseHistoryModel *model11 = [[CaseHistoryModel alloc]init];
        model11.name = @"高血脂";
        model11.select = NO;
        [_arrDataSource addObject:model11];
        
        CaseHistoryModel *model12 = [[CaseHistoryModel alloc]init];
        model12.name = @"高血糖";
        model12.select = NO;
        [_arrDataSource addObject:model12];
        
        CaseHistoryModel *model13 = [[CaseHistoryModel alloc]init];
        model13.name = @"贫血";
        model13.select = NO;
        [_arrDataSource addObject:model13];
        
        CaseHistoryModel *model14 = [[CaseHistoryModel alloc]init];
        model14.name = @"哮喘";
        model14.select = NO;
        [_arrDataSource addObject:model14];
        
        CaseHistoryModel *model15 = [[CaseHistoryModel alloc]init];
        model15.name = @"癫痫";
        model15.select = NO;
        [_arrDataSource addObject:model15];
        
        CaseHistoryModel *model16 = [[CaseHistoryModel alloc]init];
        model16.name = @"其他";
        model16.select = NO;
        [_arrDataSource addObject:model16];
    
    }
    return _arrDataSource;
}


#pragma mark - 
- (void)save{
    NSMutableArray *arr = [NSMutableArray array];
    for (int i = 0 ; i < _arrDataSource.count; i++) {
        CaseHistoryModel *model = _arrDataSource[i];
        if (model.select) {
            [arr addObject:model.name];
        }
    }
    [self.deleaget backSelect:arr];
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
